{% extends "base.html" %}

{% block title %}照护计划分表 - 长者照护计划系统{% endblock %}

{% block page_title %}照护计划分表{% endblock %}

{% block extra_css %}
<style>
    .filter-section {
        margin-bottom: 20px;
        padding: 15px;
        border: 1px solid #dee2e6;
        border-radius: 5px;
        background-color: #f8f9fa;
    }
    
    .time-slot-table {
        font-size: 0.9rem;
    }
    
    .time-slot-table th, .time-slot-table td {
        padding: 0.5rem;
    }
    
    .time-slot-row:nth-child(odd) {
        background-color: rgba(0, 0, 0, 0.05);
    }
    
    .time-slot-row:hover {
        background-color: rgba(0, 123, 255, 0.1);
    }
</style>
{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title">照护计划分表</h5>
            </div>
            <div class="card-body">
                <div class="filter-section">
                    <form id="timeSlotFilterForm">
                        <div class="row">
                            <div class="col-md-3">
                                <label for="elder" class="form-label">老人</label>
                                <input type="text" class="form-control" id="elder" name="elder" placeholder="输入老人ID">
                            </div>
                            <div class="col-md-3">
                                <label for="date" class="form-label">日期</label>
                                <input type="date" class="form-control" id="date" name="date" value="{{ date.today().isoformat() }}">
                            </div>
                            <div class="col-md-3">
                                <label for="time_slot" class="form-label">时间段</label>
                                <input type="text" class="form-control" id="time_slot" name="time_slot" placeholder="例如：08:00-08:30">
                            </div>
                            <div class="col-md-3">
                                <label class="form-label">&nbsp;</label>
                                <div class="d-grid">
                                    <button type="button" id="filterButton" class="btn btn-primary">筛选</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-bordered time-slot-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>老人</th>
                                <th>时间段</th>
                                <th>活动</th>
                                <th>活动ID</th>
                                <th>护理员</th>
                                <th>护理员ID</th>
                                <th>日期</th>
                                <th>批次号</th>
                            </tr>
                        </thead>
                        <tbody id="timeSlotTableBody">
                            {% for slot in slots %}
                            <tr class="time-slot-row" data-elder="{{ slot.elder }}" data-date="{{ slot.schedule_date }}" data-time-slot="{{ slot.time_slot }}">
                                <td>{{ slot.id }}</td>
                                <td>{{ slot.elder }}</td>
                                <td>{{ slot.time_slot }}</td>
                                <td>{{ slot.activity }}</td>
                                <td>{{ slot.activity_id }}</td>
                                <td>{{ slot.caregiver }}</td>
                                <td>{{ slot.caregiver_id }}</td>
                                <td>{{ slot.schedule_date }}</td>
                                <td>{{ slot.batch_number if slot.batch_number else '无' }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                
                <div class="d-flex justify-content-between align-items-center mt-3">
                    <div>
                        <span>显示 <span id="visibleCount">{{ slots|length }}</span> 条记录，共 <span id="totalCount">{{ slots|length }}</span> 条记录</span>
                    </div>
                    <div>
                        <button id="exportButton" class="btn btn-success">
                            <i class="fas fa-file-excel"></i> 导出到Excel
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    $(document).ready(function() {
        // 记录总数
        const totalCount = $('.time-slot-row').length;
        $('#totalCount').text(totalCount);
        
        // 筛选按钮点击事件
        $('#filterButton').click(function() {
            const elder = $('#elder').val().toLowerCase();
            const date = $('#date').val();
            const timeSlot = $('#time_slot').val().toLowerCase();
            
            // 筛选表格行
            let visibleCount = 0;
            $('.time-slot-row').each(function() {
                const rowElder = $(this).data('elder').toString().toLowerCase();
                const rowDate = $(this).data('date');
                const rowTimeSlot = $(this).data('time-slot').toLowerCase();
                
                const elderMatch = !elder || rowElder.includes(elder);
                const dateMatch = !date || rowDate.includes(date);
                const timeSlotMatch = !timeSlot || rowTimeSlot.includes(timeSlot);
                
                if (elderMatch && dateMatch && timeSlotMatch) {
                    $(this).show();
                    visibleCount++;
                } else {
                    $(this).hide();
                }
            });
            
            // 更新显示计数
            $('#visibleCount').text(visibleCount);
        });
        
        // 导出到Excel按钮点击事件
        $('#exportButton').click(function() {
            // 获取可见行
            const visibleRows = $('.time-slot-row:visible');
            
            // 如果没有可见行，则提示
            if (visibleRows.length === 0) {
                alert('没有可导出的数据');
                return;
            }
            
            // 创建CSV内容
            let csvContent = 'ID,老人,时间段,活动,活动ID,护理员,护理员ID,日期,批次号\n';
            
            visibleRows.each(function() {
                const id = $(this).find('td:eq(0)').text();
                const elder = $(this).find('td:eq(1)').text();
                const timeSlot = $(this).find('td:eq(2)').text();
                const activity = $(this).find('td:eq(3)').text();
                const activityId = $(this).find('td:eq(4)').text();
                const caregiver = $(this).find('td:eq(5)').text();
                const caregiverId = $(this).find('td:eq(6)').text();
                const date = $(this).find('td:eq(7)').text();
                const batchNumber = $(this).find('td:eq(8)').text();
                
                csvContent += `${id},${elder},${timeSlot},${activity},${activityId},${caregiver},${caregiverId},${date},${batchNumber}\n`;
            });
            
            // 创建下载链接
            const encodedUri = encodeURI('data:text/csv;charset=utf-8,' + csvContent);
            const link = document.createElement('a');
            link.setAttribute('href', encodedUri);
            link.setAttribute('download', '照护计划分表.csv');
            document.body.appendChild(link);
            
            // 点击下载链接
            link.click();
            
            // 移除下载链接
            document.body.removeChild(link);
        });
    });
</script>
{% endblock %}
